package com.zlll.winner.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.LoginUser;

import com.zlll.winner.business.model.sysUser.SysMenu;
import com.zlll.winner.business.model.sysUser.SysUser;
import com.zlll.winner.business.model.sysUser.SysUserRole;
import com.zlll.winner.business.service.sysUser.ISysMenuService;
import com.zlll.winner.business.service.sysUser.ISysUserRoleService;
import com.zlll.winner.business.service.sysUser.ISysUserService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.common.BaseResult;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.apache.dubbo.config.annotation.Reference;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动管理控制器
 */
@RestController
@RequestMapping(value = "/sys/user")
public class SysUserController extends BaseController {

    @Reference
    private ISysUserService userService;
    @Reference
    private ISysMenuService iSysMenuService;
    @Reference
    private ISysUserRoleService iSysUserRoleService;

    /**
     * 登陆方法
     * @param params
     * @return
     */
    @PostMapping(value = "login")
    public Object login(@RequestBody JSONObject params){
        String userName = params.getString("userName");
        String password = params.getString("password");
        Assert.notNull(userName,"用户名不能为空！");
        Assert.notNull(password,"密码不能为空！");
        SysUser user = userService.login(userName,password);
        if(user != null){
            session.setAttribute("user",user);
        }
        return user;
    }

    /**
     * 查询用户信息 pageNo 页码 pageSize 页数
     * @param data
     * @return
     */
    @PostMapping(value = "list")
    @LoginUser
    public BaseResult page(@RequestBody JSONObject data){
        BaseResult baseResult = new BaseResult();
        Integer pageNo = data.getInteger("pageNo");
        Integer pageSize = data.getInteger("pageSize");
        SysUser user = data.toJavaObject(SysUser.class);
        IPage<SysUser> pages = userService.findUserPage(user, pageNo, pageSize);
        baseResult.setData(pages);
        return baseResult;
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @PostMapping(value = "getCurrentUser")
    @LoginUser
    public Object getCurrentUser(){
        return getUser();
    }

    /**
     * 获取菜单方法
     * @return
     */
    @PostMapping(value = "getMenus")
    @LoginUser
    public Object getUserMenuTree(@RequestBody JSONObject data){
        Integer needButton = data.getInteger("needButton");
        return iSysMenuService.getUserMenuTree(getUserId(),"LHT",needButton == 1);
    }

    /**
     * 保存用户信息
     * 2017-5-9 下午2:25:55
     * @param data
     * @return
     * @author lubingbing
     */
    @PostMapping(value = "save")
    @LoginUser
    public BaseResult save(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        SysUser user = data.toJavaObject(SysUser.class);
        Long roleId = data.getLong("roleId");
        userService.addUser(user, roleId, getUserId(), getUser().getName());
        return baseResult;
    }

    /**
     * 更新用户方法
     * 2017-5-9 下午2:25:55
     * @param data
     * @return
     * @author lubingbing
     */
    @PostMapping(value = "update")
    @LoginUser
    public BaseResult update(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        SysUser user = data.toJavaObject(SysUser.class);
        Long roleId = data.getLong("roleId");
        user.setPassword(null);
        userService.updateUser(user, roleId, getUserId(), getUser().getName());
        return baseResult;
    }

    /**
     * 获取用户的角色信息
     * 2017-5-9 下午2:25:55
     * @param data
     * @return
     * @author lubingbing
     */
    @PostMapping(value = "getUserRole")
    public BaseResult getUserRole(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long userId = data.getLong("userId");
        List<SysUserRole> list = iSysUserRoleService.getSysUserRoleByUserId(userId);
        baseResult.setData(list);
        return baseResult;
    }

    /**
     * 删除数据
     * @param data
     * @return
     */
    @PostMapping(value = "delete")
    @LoginUser
    public BaseResult deleteUser(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long[] ids = data.getJSONArray("ids").toJavaObject(Long[].class);
        for(Long id: ids){
            userService.deleteUser(id, getUserId(), getUser().getName());
        }
        return baseResult;
    }

    /**
     * 获取用户按钮权限
     * @return
     */
    @PostMapping(value = "getUserButtonMenus")
    public Object getUserButtonMenus(){
        List<SysMenu> list = iSysMenuService.getUserMenuList(getUserId(),"LHT", true);
        List<String> lists = list.stream().filter(item->item.getButton()==1).map(SysMenu::getUrl).collect(Collectors.toList());
        return lists;
    }

    /**
     * 重置密码
     * @param data
     * @return
     */
    @PostMapping(value = "reset")
    public BaseResult reset(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long[] ids = data.getJSONArray("ids").toJavaObject(Long[].class);
        String password = data.getString("password");
        userService.resetPassword(ids, password, getUserId(), getUser().getName());
        return baseResult;
    }

}
